package com.firestorm.eagletrons.login;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.firestorm.eagletrons.official.R;

public class LoginActivity extends Activity {
	EditText un, pw;
	Button ok;
	ProgressDialog pd;
	ArrayList<NameValuePair> loginInfo;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		// getWindow().setLayout(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
		un = (EditText) findViewById(R.id.et_un);
		pw = (EditText) findViewById(R.id.et_pw);
		ok = (Button) findViewById(R.id.btn_login);
		getWindow().setLayout(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);
		final Handler mHandler = new Handler();
		final Runnable toast_failed = new Runnable() {
			public void run() {
				Toast toast = Toast.makeText(getApplicationContext(),
						"Incorrect username or password.", Toast.LENGTH_SHORT);
				toast.show();
			}
		};
		final Runnable toast_success = new Runnable() {
			public void run() {
				Toast toast = Toast.makeText(getApplicationContext(),
						"Successful Login.", Toast.LENGTH_SHORT);
				toast.show();
				SharedPreferences settings = getSharedPreferences("eagletrons",
						0);
				SharedPreferences.Editor editor = settings.edit();
				editor.putBoolean("logged_in", true);
				editor.putString("username", un.getText().toString());
				editor.putString("password", pw.getText().toString());
				editor.commit();
				finish();
			}
		};

		ok.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				loginInfo = new ArrayList<NameValuePair>();
				loginInfo.add(new BasicNameValuePair("username", un.getText()
						.toString()));
				loginInfo.add(new BasicNameValuePair("password", pw.getText()
						.toString()));
				pd = ProgressDialog.show(LoginActivity.this, "Logging in..",
						"Logging you in...", true, false);

				Thread thread = new Thread() {
					public void run() {

						try {

							// String valid = "1";
							String response = null;
							try {
								response = CustomHttpClient.executeHttpPost(
										"http://eagletrons.net/application/android/login.php",
										loginInfo);
								String res = response.toString();
								// res = res.trim();
								res = res.replaceAll("\\s+", "");
								// error.setText(res);

								if (res.equals("1")) {
									mHandler.post(toast_success);
								} else {
									mHandler.post(toast_failed);
								}
							} catch (Exception e) {
								e.printStackTrace();

							}
						} catch (Exception e) {
							e.printStackTrace();
						}

						// dismiss the progress dialog

						pd.dismiss();

					}

				};

				thread.start();

			}
		});
	}

}
